/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;


import Beans.NhanVien;
import Models.NgonNgu_Model;
import Models.NhanVien_Model;
import Models.ThamSoHT_Model;
import Models.NhatKyHT_Model;

/**
 *
 * @author VIC
 */
public class DangNhap_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            String action = request.getParameter("action");
            // Xử lý login
            if(action.equals("user"))
            {
                
                int langid = Integer.parseInt(request.getParameter("lang"));
                session.setAttribute("langid", langid);
            }
            else if (action.equals("clogin")) {
                if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
                }
                String user = request.getParameter("user");
                String pw = request.getParameter("pass");
                NhanVien nv = NhanVien_Model.LayThongTin(user, pw);
                if (nv != null && !nv.getHoTen().equals("") && nv.getMaNNV() == 2) {
                     session.setAttribute("nv", nv);
                     NhatKyHT_Model.GhiNhatKy(1, nv.getMaNV(), "system");
                     out.print(nv.getMaNV());
                }
                else {
                    out.print("0");
                }
            }
            else if (action.equals("clogout")) {
                session.removeAttribute("nv");
                session.removeAttribute("item");
                session.removeAttribute("DSNMA");
                session.removeAttribute("DSMA");
            }
            else if (action.equals("login")) {
                String user = request.getParameter("user");
                String pw = request.getParameter("pass");
                String lang = request.getParameter("lang");
                int langid = NgonNgu_Model.MaNgonNgu(lang);
                NhanVien nv = NhanVien_Model.LayThongTin(user, pw);
                if (nv != null && !nv.getHoTen().equals("")) {
                    session.setAttribute("nv", nv);
                    session.setAttribute("langid", langid);                    
                    NhatKyHT_Model.GhiNhatKy(1, nv.getMaNV(), "system");
                    
                    if (nv.getMaNNV() == 1) {
                        response.sendRedirect("admin/admin.jsp");
                    } else if (nv.getMaNNV() == 4) {
                        response.sendRedirect("cook/cook.jsp");
                    } else if (nv.getMaNNV() == 3) {
                        response.sendRedirect("cashier/cashier.jsp");
                    } else {
                        response.sendRedirect("login.jsp");
                    }
                } else {
                    // Biến msgcontent lưu nội dung thông báo.
                    String msgcontent = ThamSoHT_Model.TenThamSoHT("loginerror", langid);
                    // Biến msgtitle lưu tiêu đề thông báo.
                    String msgtitle = ThamSoHT_Model.TenThamSoHT("notice", langid);
                    request.setAttribute("title", msgtitle);
                    request.setAttribute("content", msgcontent);
                    RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
                    rd.forward(request, response);
                }
            } // Xử lý logout
            else if (action.equals("logout")) {
                //if(session != null)
                
                session.removeAttribute("nv");
                session.removeAttribute("langid");
                session.removeAttribute("item");
                session.removeAttribute("DSNMA");
                session.removeAttribute("DSMA");
                response.sendRedirect("login.jsp");
            } else {
                response.sendRedirect("login.jsp");
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
